package com.goldsprite.projecttemplategenerator.android;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.core.content.FileProvider;

import java.io.File;

public class TestAndroidLauncher extends Activity {
	private AndroidApi androidApi;
	private TestAndroidLauncher instance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;

		androidApi = new AndroidApi(this);

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		Button button = new Button(this);
		layout.addView(button);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				File apkFile = new File(getFilesDir(), "project-template-generator-0.1.1.apk");
				Uri apkUri;
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
					apkUri = FileProvider.getUriForFile(instance, instance.getPackageName() + ".fileprovider", apkFile);
				} else {
					apkUri = Uri.fromFile(apkFile);
				}

				intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
				instance.startActivity(intent);
			}
		});

		setContentView(layout);
	}
}
